/*!
 * Page: pages/information/list/List
 * License: MIT
 * Created: 2019-11-08 19:04
 */

import { informationApi } from '../../../api/information'
import { GlobalData } from '../../../store/state'
import { $event } from '../../../utils'

Page({
  data: {
    currentTab: 0,
    navData: [{ id: 0, name: '推荐', page: 1, isLastPage: false }] as any[],
    currentTabList: [] as number[],
    list: [] as any[],
    lists: [] as any[],
    access: GlobalData.access,
    navScrollLeft: 0,
  },
  async onLoad() {
    const navData = await informationApi.getInformationCategories()
    navData.map((item: { page: number }) => (item.page = 1))
    this.setData({
      navData: [...this.data.navData, ...navData],
    })
  },
  async onShow() {
    await this.onReset()
    $event.on('after:boot', this, () => {
      this.setData({ access: GlobalData.access })
    })
    this.setData({ access: GlobalData.access })
  },
  switchNav(event: WechatMiniprogram.CustomEvent) {
    const windowWidth = GlobalData.clientWidth
    const cur = event.currentTarget.dataset.current
    //每个tab选项宽度占1/5
    const singleNavWidth = windowWidth / 5
    //tab选项居中
    this.setData({
      navScrollLeft: (cur - 2) * singleNavWidth,
    })
    if (this.data.currentTab == cur) {
      return
    } else {
      this.setData({
        currentTab: cur,
      })
      this.switchList()
    }
  },
  switchList() {
    const cur = this.data.currentTab
    if (this.data.currentTabList.indexOf(cur) === -1) {
      this.onReset()
    }
  },

  async onPullDownRefresh() {
    await this.onReset()
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    this.onLoadmore()
  },
  async onReset() {
    const currentTab = +this.data.currentTab
    const page = 1
    const list =
      currentTab === 0
        ? await informationApi.getInformationCategoriesList({
          page: page,
          order: 'topped_at desc,recommend_at desc,id desc',
          limit: 20,
          with: 'author',
          recommend: 1,
        })
        : await informationApi.getInformationCategoriesList({
          category_id: this.data.navData[currentTab].id,
          page: page,
          order: 'category_topped_at desc,id desc',
          limit: 20,
          with: 'author',
        })
    const isLastPage =
      list.meta.last_page === list.meta.current_page || list.meta.last_page < list.meta.current_page
    const currentTabList = this.data.currentTabList
    currentTabList.push(this.data.currentTab)
    this.setData({
      [`list[${currentTab}]`]: list.data,
      [`navData[${currentTab}].isLastPage`]: isLastPage,
      currentTabList: currentTabList,
      [`navData[${currentTab}].page`]: 1,
      [`navData[${currentTab}].show`]: list.data.length === 0 ? true : false,
    })
    wx.hideLoading()
  },
  async onLoadmore() {
    if (this.data.navData[this.data.currentTab].isLastPage) {
      wx.showToast({
        title: '已没有更多了',
        icon: 'none',
        duration: 1000,
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    const currentTab = +this.data.currentTab
    const categoryId = this.data.currentTab
    const page = this.data.navData[categoryId].page + 1
    const list =
      currentTab === 0
        ? await informationApi.getInformationCategoriesList({
          page: page,
          order: 'topped_at desc,recommend_at desc,id desc',
          limit: 20,
          with: 'author',
        })
        : await informationApi.getInformationCategoriesList({
          category_id: this.data.navData[currentTab].id,
          page: page,
          order: 'category_topped_at desc,id desc',
          limit: 20,
          with: 'author',
        })
    const isLastPage =
      list.meta.last_page === list.meta.current_page || list.meta.last_page < list.meta.current_page
    this.setData({
      [`list[${categoryId}]`]: [...this.data.list[categoryId], ...list.data],
      [`navData[${categoryId}].page`]: list.meta.current_page,
      [`navData[${categoryId}].isLastPage`]: isLastPage,
    })
    wx.hideLoading()
  },
})
